<?php
class User extends CI_Controller{
    function __construct(){
        parent::__construct();

    }
    function index(){
        $this->load->model('Model_user');
        if($this->Model_user->is_logged_in()){
            $user_id=$this->session->userdata("user_id");
            $user=$this->Model_user->user_info($user_id);
            $data['user_name']=$user->user_name;

            $this->load->view('user/user_index',$data);
           
        }else{
            redirect("user/login");
        }
         $this->load->view('user/index');
    }
    function add_new_student($add=null){
        if(empty($add)){
            if(isset($_POST['submit'])){
                $student_name=$_POST['student_name'];
                $nick_name=$_POST['nick_name'];
                $password=$_POST['password'];
                $sex=$_POST['sex'];
                $birthday[]=$_POST['year'];
                $birthday[]=$_POST['month'];
                $birthday[]=$_POST['day'];
                $birthday=  implode("-", $birthday);
                $phone_number=$_POST['phone_number'];
                $email=$student_info=$_POST['email'];
            }
            $this->load->model('Model_user');
            $result=$this->Model_user->add_new_student(NULL,$student_name,$nick_name,$password,$sex,$birthday,$phone_number,$email);
            if($result){
                redirect("user/show_all_student");
            }else{
                echo 'no....';
            }
        }else{
             $this->load->view('user/add_new_student');
        }
    }
    function delete_student(){
        if(isset($_GET['submit'])){
            $student_id=$_GET['student_id'];
            $this->load->model('Model_user');
            $result=$this->Model_user->delete_student($student_id);
            if($result){
                echo 'succeed';
            }else{
                echo 'fail';
            }
        }
    }
        function show_all_student(){
            $this->load->model('Model_user');
            $result=$this->Model_user->show_all_student();
            $data['row']=$result->result_array();
            $this->load->view('user/show_all_student',$data);
        }
        function show_one_student($student_id){
            $this->load->model('Model_user');
            $result=$this->Model_user->show_one_student($student_id);
            foreach ($result->result() as $row){
                echo $row->student_name;
            }
        }
    
        function update_student(){
            if(isset($_POST['submit'])){
            $student_id=$_POST['student_id'];
            $phone_number=$_POST['phone_number'];
            $email=$student_info=$_POST['email'];
        }
            $this->load->model('Model_user');
            $result=$this->Model_user->update_student($student_id,$phone_number,$email);
            if($result){
                echo 'oye~~';
            }else{
                echo 'no....';
            }
    }

    function open_new_week(){
        $this->load->view('schedule/open_new_week');
    }
    
    function check_status(){
        if(isset($_POST['submit'])){
            $user_name = trim($_POST['user_name']);
            $password = trim($_POST['password']);
             $this->load->model('Model_user');
            $found_user_id=$this->Model_user->authenticate($user_name,$password);

            if($found_user_id){
                $this->Model_user->login($found_user_id);
                redirect("user/index");
            }else{
                redirect("user/login");
            }            
        }
    }
    public function login(){
        $this->load->view('user/login');
    }
    function logout(){
        $this->load->model('Model_user');
        $this->Model_user->logout();     
    }
}
    ?>
